<!--评审意见弹窗-->
<template>
  <div>
    <rk-dialog width="480"
      height="340"
      :title="title"
      :visible="localVisible"
      :destroyOnClose="true"
      :maskClosable="false"
     
      @close="handleClose">
      <div  v-loading="confirmLoading">
        <el-form ref="examineIdeasModalRef"
          :model="form"
          :rules="baseConfRules"
          label-width="auto">
          <el-form-item label="满足发债要求:"
            prop="isMatchCondition">
            <el-radio-group v-model="form.isMatchCondition"
              :disabled="isOnevoteVetoVisible">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="评审意见"
            prop="remark">
            <el-input type="textarea"
              rows="4"
              placeholder="请填写评审意见:"
              v-model="form.remark" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button type="primary"
          @click.stop="handleOk">确定
        </el-button>
        <el-button @click="handleClose">取消</el-button>
      </template>
    </rk-dialog>
  </div>
</template>

<script setup name="ExamineIdeasModal">
import { ElMessage } from 'element-plus'
import { reactive, ref, watch } from "vue"
import { useOnlineApi } from '@/api/index';
const { saveDebtReviewScore } = useOnlineApi();
const emits = defineEmits(['update:visible', 'closeAll']);
const isOnevoteVetoVisible = ref(false);
const form = reactive({
  remark: '',
  isMatchCondition: 1
})
const confirmLoading = ref(false)
const baseConfRules = reactive({
  isMatchCondition: [
    { required: true, message: '请选择是否满足发债要求', trigger: 'change' }
  ],
  remark: [
    { required: false, message: '请填写评审意见', trigger: 'blur' }
  ]
})

const props = defineProps({
  visible: {
    type: Boolean,
  },
  operatorType: {
    type: [String,Number],
    default: " "
  },
  reviewTaskType: {
    type: [String,Number],
    default: " "
  },
  activityTaskId: {
    type: [String,Number],
    default: " "
  },
  applyId: {
    type: String,
    default: " "
  },
  taskId: {
    type: String,
    default: " "
  },
  submitScoreItems: {
    type: Array,
  },
  refresh: {
    type: [Promise, Function],
    default: () => {
      return new Promise(resolve => resolve())
    }
  },
})
const localVisible = ref(props.visible)
watch(localVisible, (v) => {
  emits('update:visible', v)
})

watch(() => props.visible, (v) => {
  localVisible.value = v
})
// 回显数据
const setFormData = ({ ...formData }) => {
  const { scoringItems } = formData
  let onevoteVetoeArray = []
  scoringItems.forEach((item) => {
    if (item.isOnevoteVeto == 1) {
      onevoteVetoeArray.push(item.score)
    }
  })
  if (onevoteVetoeArray.indexOf("否") > -1) {
    isOnevoteVetoVisible.value = true
    ElMessage({ message: "涉及一票否决评分项，不符合发债要求！", type: "error" });
    formData.isMatchCondition = 0
  } else {
    isOnevoteVetoVisible.value = false
    formData.isMatchCondition = 1
  }
  form.remark = formData.remark
  form.isMatchCondition = formData.isMatchCondition
}

/** 确定事件 */
const handleOk = () => {
  handleConfirm()
  // props.refresh();
}
const handleConfirm = async () => {
  await examineIdeasModalRef.value.validate((valid, fields) => {
    if (valid) {
      confirmLoading.value = true;
      let formData = {
        "operatorType":props.operatorType,
        "reviewTaskType": props.reviewTaskType,
        "activityTaskId": props.activityTaskId,
        "applyId": props.applyId,
        "taskId": props.taskId,
        "isMatchCondition": form.isMatchCondition,
        "expertOpinion": form.remark,
        "scoreItemList": props.submitScoreItems
      }
      //提交评审
      submitReview(formData);
    } else {
      confirmLoading.value = false;
    }
  })
}

const submitReview = async (formData) => {
  const result = await saveDebtReviewScore(formData)
  const { data, code, msg } = result
  if (code === 200) {
    handleCloseAll();
    ElMessage({ message: msg ?? '评审成功！', type: "success" });
  } else {
    handleClose();
    ElMessage({ message: msg ?? '评审失败！', type: "error" });
  }
}

const examineIdeasModalRef = ref(null);
const handleClose = () => {
  form.remark = ''
  form.isMatchCondition = 1
  localVisible.value = false;
}

const handleCloseAll = () => {
  //关闭评审弹窗
  handleClose();
  emits('closeAll')
}

// 对外暴露方法
defineExpose({
  setFormData
})

const title = ref('填写评审意见')
</script>

<style lang="scss" scoped>
</style>
